<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>easyCMS</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/resource/layuimini/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/resource/layuimini/css/public.css" media="all">
    <style>
        body{
            background: #fff;
        }
        form{
            width: 600px;
            margin: 0 auto;
        }
        .layui-form-label{
            width: 140px;
        }
        .layui-form-item .layui-input-inline {
            width: 250px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" lay-filter="form" id="form">
            <input type="hidden" id='id' name='id' value="">
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>上级菜单</label>
                <div class="layui-input-inline">
                    <div id="parentidTree"></div>
                    <input type="hidden" id='pid' name='pid' value="">
                    <input type="hidden" id='level' name='level' value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>菜单名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" lay-verify="required" placeholder="请输入菜单名称" maxlength="50" autocomplete="off" class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label"><span class="x-red">*</span>菜单类型</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="type" value="1" title="菜单">
                        <input type="radio" name="type" value="2" title="按钮">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">请求地址</label>
                    <div class="layui-input-inline">
                        <input type="text" id="href" name="href" placeholder="请输入请求地址" maxlength="100" autocomplete="off" class="layui-input" />
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">权限标识</label>
                <div class="layui-input-inline">
                    <input type="text" id="permits" name="permits" placeholder="请输入权限标识" maxlength="100" autocomplete="off" class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>打开方式</label>
                <div class="layui-input-inline">
                    <input type="radio" name="target" value="_self" title="框架内">
                    <input type="radio" name="target" value="_blank" title="新窗口">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">图标</label>
                <div class="layui-input-inline">
                    <input type="text" id="icon" name="icon" placeholder="" maxlength="20" autocomplete="off" class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>排序</label>
                <div class="layui-input-inline">
                    <input type="text" id="sort" name="sort" placeholder="请输入排序值" maxlength="3" autocomplete="off" class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="smBtn">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>


    </div>
</div>

<script src="/resource/layuimini/lib/layui/layui.js" charset="utf-8"></script>
<script src="/resource/js/xm-select.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        let form = layui.form
        let $ = layui.jquery
        form.val("form", {
            "id": "{$menu->id|default=''}",
            "pid": "{$menu->pid|default=''}",
            "level": "{$menu->level|default=''}",
            'title': "{$menu->title|default=''}",
            'type': "{$menu->type|default='1'}",
            'permits': "{$menu->permits|default=''}",
            'href': "{$menu->href|default=''}",
            'icon': "{$menu->icon|default=''}",
            'target': "{$menu->target|default='_self'}",
            'sort': "{$menu->sort|default='100'}"
        });
        xmSelect.render({
            el: '#parentidTree',
            language: 'zn',
            radio: true,
            clickClose: true,
            height: '500px',
            prop:{
                name:'title'
            },
            tree: {
                show: true,
                showFolderIcon: true,
                showLine: true,
                indent: 20,
                strict: false,
                expandedKeys: true,
            },
            initValue: [{$menu->pid|default=''}],
            data: <?= json_encode($menuAll); ?>,
            on: function(data){
                let arr = data.arr;
                if(arr.length>0){
                    $("#pid").val(arr[0].value);
                    $("#level").val(arr[0].level);
                } else {
                    $("#pid").val('');
                    $("#level").val('');
                }
            }
        })
        //监听提交
        form.on('submit(smBtn)', function(data){
            let id = $("#id").val()
            let url = ''
            if (id == '')
            {
                url = "/admin/menu/doMenuAdd"
            }else{
                url ="/admin/menu/doMenuEdit"
            }
            $.post(url, $("#form").serialize(), function(resp){
                parent.layer.alert(resp.msg,function(index){
                    if(resp.code == 0)
                    {
                        let iframe = parent.document.querySelector('.layui-show').querySelector('iframe')
                        iframe.contentWindow.layui.treeTable.reload('listTable')
                        parent.layer.closeAll()
                    }else {
                        parent.layer.close(index)
                    }
                });
            }, "json");
        });
    });
</script>

</body>
</html>